package com.isomentricengine.map
{
	import com.isomentricengine.character.BaseCharacter;
	import com.isomentricengine.tile.*;
	import com.medusa.core.extension.BaseExtension;
	
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	public class LevelMap extends BaseExtension implements IMap
	{
		private var dummyFloorTile:BackgroundTile;
		private var dummyWallTile:SolidTile;
		private var dummyDoorTile:DoorTile;
		private var dummyCloudTile:CloudTile;
		private var dummyWallLadderTile:SolidLadderTile;
		private var dummyFloorLadderTile:BackgroundLadderTile;
		
		private var mapArray:Array;
		private var controllableCharacter:BaseCharacter;
		private var currentKey:uint;
		private var tileType:Dictionary;
		
		protected var _mapIndex:Number;
		protected var _tileW:Number;
		protected var _tileH:Number;
		protected var _tiles:Array;
		protected var _mapWidth:Number;
		protected var _mapHeight:Number;

		
		public function LevelMap(map:Array, tileWidth:Number, tileHeight:Number)
		{
			mapArray = map;
			tileH = tileHeight;
			tileW = tileWidth;
			
			tileType = new Dictionary();
			tileType[0] = "com.isomentricengine.tile.BackgroundTile";
			tileType[1] = "com.isomentricengine.tile.SolidTile";
			tileType[2] = "com.isomentricengine.tile.CloudTile";
			tileType[3] = "com.isomentricengine.tile.BackgroundLadderTile";
			tileType[4] = "com.isomentricengine.tile.SolidLadderTile";
			tileType[5] = "com.isomentricengine.tile.DoorTile";
		}
		
		public function build():Array
		{
			mapWidth = mapArray[0].length;
			mapHeight = mapArray.length;
			
			tiles = [];
			
			for(var a:Number = 0; a<mapHeight; a++){
				addTilesByRow(mapArray[a], a);
			}
			
			return tiles;
		}
		
		public function destroy():void
		{
			tiles = [];
		}
		
		private function addTilesByRow(row:Array, rowNumber:Number):void
		{
			for(var columnNumber:Number = 0; columnNumber<row.length; columnNumber++){
				var walkable:Boolean = (row[columnNumber] != 1) ? true : false;
				var isDoor:Boolean = (row[columnNumber] == 5) ? true : false;
				var xPos:Number = columnNumber * tileW;
				var yPos:Number = rowNumber * tileH;
				
				var TileType:Class = getDefinitionByName(tileType[row[columnNumber]]) as Class;
				var tile:* = new TileType(tileW, tileH);
				
				tile.name = "tile_"+ rowNumber + "_" + columnNumber;
				tile.column = columnNumber;
				tile.row = rowNumber;
				tile.x = xPos;
				tile.y = yPos;
				tile.build();
				
				tiles.push(tile);
			}
		}
		
		public function get tileW():Number
		{
			return _tileW;
		}

		public function set tileW(value:Number):void
		{
			_tileW = value;
		}

		public function get tileH():Number
		{
			return _tileH;
		}

		public function set tileH(value:Number):void
		{
			_tileH = value;
		}
		
		public function get tiles():Array
		{
			return _tiles;
		}

		public function set tiles(value:Array):void
		{
			_tiles = value;
		}
		
		public function get mapIndex():Number
		{
			return _mapIndex;
		}

		public function set mapIndex(value:Number):void
		{
			_mapIndex = value;
		}
		
		public function get mapWidth():Number
		{
			return _mapWidth;
		}

		public function set mapWidth(value:Number):void
		{
			_mapWidth = value;
		}

		public function get mapHeight():Number
		{
			return _mapHeight;
		}

		public function set mapHeight(value:Number):void
		{
			_mapHeight = value;
		}
		
	}
}